<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 分割均衡字符串（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>均衡串定义&#xff1a;字符串中只包含两种字符&#xff0c;且这两种字符的个数相同。</p> 
<p>给定一个均衡字符串&#xff0c;请给出可分割成新的均衡子串的最大个数。</p> 
<p>约定&#xff1a;字符串中只包含大写的 X 和 Y 两种字符。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入一个均衡串。</p> 
<ul><li>字符串的长度&#xff1a;[2&#xff0c; 10000]。</li><li>给定的字符串均为均衡字符串</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出可分割成新的均衡子串的最大个数。</p> 
<p></p> 
<h4>备注</h4> 
<p>分割后的子串&#xff0c;是原字符串的连续子串</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:550px;"><tbody><tr><td>输入</td><td>XXYYXY</td></tr><tr><td>输出</td><td>2</td></tr><tr><td>说明</td><td>XXYYXY可分割为2个均衡子串&#xff0c;分别为&#xff1a;XXYY、XY</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题要求分割出最多的均衡子串&#xff0c;含义其实是分割出来的均衡子串无法再分解。</p> 
<p>比如用例 &#34;XXYYXY&#34; 分解出来的两个子串 &#34;XXYY&#34; 和 &#34;XY&#34; 都是无法再次分解出均衡子串的。</p> 
<p></p> 
<p>如果我们从一个均衡串中取走一个的均衡子串&#xff0c;则均衡串剩余部分依旧是一个均衡串&#xff0c;因为取走的均衡子串中X和Y的数量是相等&#xff0c;因此均衡串剩余部分的X和Y数量也一定是相等的&#xff0c;满足均衡串要求。</p> 
<p></p> 
<p>因此&#xff0c;本题我们只需要从左往右扫描输入的均衡串每个字符即可&#xff0c;统计扫描过程中&#xff0c;X字符和Y字符的数量&#xff0c;每当两种字符数量相等时&#xff0c;则说明遇到了一个不可分解的均衡子串。</p> 
<p><img alt="" height="670" src="https://img-blog.csdnimg.cn/76fde25d0834444790a621cf2d6ffa2c.png" width="844" /></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const s &#61; await readline();

  let countX &#61; 0;
  let countY &#61; 0;

  let ans &#61; 0;

  for (let c of s) {
    if (c &#61;&#61; &#34;X&#34;) {
      countX&#43;&#43;;
    } else {
      countY&#43;&#43;;
    }

    if (countX &#61;&#61; countY) {
      ans&#43;&#43;;
    }
  }

  console.log(ans);
})();
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String s &#61; sc.nextLine();

    int countX &#61; 0;
    int countY &#61; 0;

    int ans &#61; 0;

    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      if (s.charAt(i) &#61;&#61; &#39;X&#39;) {
        countX&#43;&#43;;
      } else {
        countY&#43;&#43;;
      }

      if (countX &#61;&#61; countY) {
        ans&#43;&#43;;
      }
    }

    System.out.println(ans);
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()


# 算法入口
def getResult():
    countX &#61; 0
    countY &#61; 0

    ans &#61; 0

    for c in s:
        if c &#61;&#61; &#39;X&#39;:
            countX &#43;&#61; 1
        else:
            countY &#43;&#61; 1

        if countX &#61;&#61; countY:
            ans &#43;&#61; 1

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

int main() {
    char s[10001];
    scanf(&#34;%s&#34;, s);

    int countX &#61; 0;
    int countY &#61; 0;

    int ans &#61; 0;

    int i &#61; 0;
    while (s[i] !&#61; &#39;\0&#39;) {
        if (s[i] &#61;&#61; &#39;X&#39;) {
            countX&#43;&#43;;
        } else {
            countY&#43;&#43;;
        }

        if (countX &#61;&#61; countY) {
            ans&#43;&#43;;
        }

        i&#43;&#43;;
    }

    printf(&#34;%d\n&#34;, ans);

    return 0;
}</code></pre> 
<p></p> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>